<?php

/*
	project should inherit from this class and supply a valid table name unique to that project.
*/

class spOrderDatabase extends spFormDatabase
{
	const MERCHANDISE_STATUS_NONE 		= 0;	// status has not been defined
	const MERCHANDISE_STATUS_WAITING 	= 1;	// buyer is waiting for their merchandise
	const MERCHANDISE_STATUS_RECEIVED 	= 2;	// buyer has received their merchandise
	
	const PAYMENT_STATUS_NONE			= 0;	// status has not been defined
	const PAYMENT_STATUS_COMPLETED		= 1;	// payment is completed
	const PAYMENT_STATUS_FAILED			= 2;	// payment has failed
	const PAYMENT_STATUS_PROCESSING		= 3;	// payment is being processed by gateway

	static protected $listMerchandiseStatus = array(
		spOrderDatabase::MERCHANDISE_STATUS_NONE => array(
			"content" 	=> "",
			"value"		=> spOrderDatabase::MERCHANDISE_STATUS_NONE
		),
		spOrderDatabase::MERCHANDISE_STATUS_WAITING => array(
			"content"	=> "Waiting for merchandise",
			"value"		=> spOrderDatabase::MERCHANDISE_STATUS_WAITING
		),
		spOrderDatabase::MERCHANDISE_STATUS_RECEIVED => array(
			"content"	=> "Has received merchandise",
			"value"		=> spOrderDatabase::MERCHANDISE_STATUS_RECEIVED
		)
	);

	static protected $listPaymentStatus = array(
		spOrderDatabase::PAYMENT_STATUS_NONE => array(
			"content" 	=> "Incomplete",
			"value"		=> spOrderDatabase::PAYMENT_STATUS_NONE
		),
		spOrderDatabase::PAYMENT_STATUS_COMPLETED => array(
			"content" 	=> "Completed",
			"value"		=> spOrderDatabase::PAYMENT_STATUS_COMPLETED
		),
		spOrderDatabase::PAYMENT_STATUS_FAILED => array(
			"content" 	=> "Failed",
			"value"		=> spOrderDatabase::PAYMENT_STATUS_FAILED
		),
		spOrderDatabase::PAYMENT_STATUS_PROCESSING => array(
			"content" 	=> "Processing",
			"value"		=> spOrderDatabase::PAYMENT_STATUS_PROCESSING
		)
	);
	
	public function __construct( $id=false )
	{
		parent::__construct( $id );
		
		if ( $id==false )
		{
			$this->addTimeStamp( new spTimeStamp("Created") ); 
			$this->setValue( "date", date("d-m-Y") );
		}
	}
	
	// @override
	static public function getName()
	{
		return "Orders";
	}	
	
	// @override
	static public function getSqlIdentity()
	{
		return "merchandiseid";
	}		
	
	// deprecated! apparently merchandiseid is used... so i have to find the sinner and correct him
	public function set( $merchandiseid, $hasreceived=true, $haspaid=false )
	{
		parent::setValue( "merchandiseid", 	$merchandiseid );
		parent::setValue( "hasreceived", 	$hasreceived );
		parent::setValue( "haspaid", 		$haspaid );
	}
	
	public function addTimeStamp( $ts )
	{
		$log = $this->getValue( "log" );
		
		if ( $log!="" )
			$log.= "\n";
			
		$log.= $ts->get();
		
		$this->setValue( "log", $log );
	}
	
	static public function getPaymentStatusText( $constStatus )
	{
		if ( $constStatus=="" )
			return spOrderDatabase::$listPaymentStatus[spOrderDatabase::PAYMENT_STATUS_NONE]["content"];;
	
		return spOrderDatabase::$listPaymentStatus[$constStatus]["content"];
	}
	
	public function setPaymentStatus( $constStatus )
	{
		// sanity check... we can not change to status "Processing" if the payment status is already "Completed".
		// this can actually happen if the payment gateway message for processing is taking longer time to come our server.
		$currentStatus = $this->getValue( "paymentstatus" );
		if ( $currentStatus==spOrderDatabase::PAYMENT_STATUS_COMPLETED && $constStatus==spOrderDatabase::PAYMENT_STATUS_PROCESSING )
		{
			// we do not change the status, but register the event in the log
			$this->addTimeStamp( new spTimeStamp("Payment Status: out of sync event: Processing. Current event stays as Completed" ) ); 
			return;
		}
	
		$this->setValue( "paymentstatus", $constStatus );
		$this->addTimeStamp( new spTimeStamp("Payment Status: ".$this->getPaymentStatusText($constStatus)) ); 
	}
	
	public function getMerchandiseStatusText( $constStatus )
	{
		return spOrderDatabase::$listMerchandiseStatus[$constStatus]["content"];
	
	}
	
	public function setMerchandiseStatus( $constStatus )
	{
		$this->setValue( "merchandisestatus", $constStatus );
		$this->addTimeStamp( new spTimeStamp("Mechandise Status: ".$this->getMerchandiseStatusText($constStatus)) ); 
	}
	
	protected function createMerchandiseDatabase()
	{
		echo "<div class='error'>ERROR: spOrderDatabase -> createMerchandiseDatabase has not been overrided!</div>";
	}
	
	// @override
	public function createFormParts()
	{
		$dbMerchandises = $this->createMerchandiseDatabase();
		$listMerchandises = $dbMerchandises->getRows(array(
			"value"		=> "id",
			"content" 	=> "title"
		));	
	
		$ar = parent::createFormParts();
	
		return $ar + array(
			"date" => array(array(
				"title"	 	=> "Date Created",
				"type"		=> "string",
				"sql_col"	=> "date",
				"readonly"	=> "true"
			)),
			"merchandiseid" => array(array(
				"title"		=> "Merchandise ID",
				"type"		=> "combobox",
				"list"		=> $listMerchandises,
				"sql_col"	=> "merchandiseid",
				"required"	=> true
			)),
			"merchandisestatus" => array(array(
				"title"	 	=> "Merchandise Status",
				"type"		=> "combobox",
				"list"		=> static::$listMerchandiseStatus,
				"sql_col"	=> "merchandisestatus"
			)),
			"paymentstatus" => array(array(
				"title"		=> "Payment Status",
				"type"		=> "combobox",
				"list"		=> static::$listPaymentStatus,
				"sql_col"	=> "paymentstatus"
			)),
			"comments" => array(array(
				"title"	 	=> "Comments",
				"type"		=> "textarea",
				"sql_col"	=> "comments"
			)),
			"log" => array(array(
				"title"	 	=> "Log",
				"type"		=> "textarea",
				"sql_col"	=> "log",
				"readonly"	=> "true"
			))
		);
	}			
}

?>